package com.supercube.sport.yoga.utils;

import com.supercube.common.utils.ShiroUtils;
import com.supercube.common.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * Description：
 * Created By guowen
 */
@Slf4j
public class UserUtils {
    public static<T> void setRecordBase(T obj){
        try{
            Long userId = ShiroUtils.getUserId();
            Field[] fields = obj.getClass().getDeclaredFields();
            for(Field field : fields){
                if(Objects.equals(field.getName(),"createTime")){
                    field.setAccessible(true);
                    field.set(obj,TimeUtils.getCurrentTime());
                }
                if(Objects.equals(field.getName(),"createUser")){
                    field.setAccessible(true);
                    field.set(obj,userId.toString());
                }
                if(Objects.equals(field.getName(),"lastModifyTime")){
                    field.setAccessible(true);
                    field.set(obj,TimeUtils.getCurrentTime());
                }
                if(Objects.equals(field.getName(),"lastModifyUser")){
                    field.setAccessible(true);
                    field.set(obj,userId.toString());
                }
            }
        }catch (Exception e){
            log.info(""+e);
        }


    }

    /**
     * 设置修改人及修改时间
     * @param obj
     * @param <T>
     */
    public static<T> void setLastModifyRecordBase(T obj){
        try{
            Long userId = ShiroUtils.getUserId();
            Field[] fields = obj.getClass().getDeclaredFields();
            for(Field field : fields){
                if(Objects.equals(field.getName(),"lastModifyTime")){
                    field.setAccessible(true);
                    field.set(obj,TimeUtils.getCurrentTime());
                }
                if(Objects.equals(field.getName(),"lastModifyUser")){
                    field.setAccessible(true);
                    field.set(obj,userId.toString());
                }
            }
        }catch (Exception e){
            log.info(""+e);
        }


    }
}
